<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统配置 - 监控系统导航平台</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/font-awesome-local.css') }}">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Arial, sans-serif;
        }
        body {
            background-color: #f5f5f5;
            color: #333;
            overflow: hidden;
            height: 100vh;
            display: flex;
            flex-direction: column;
            margin: 0;
            padding: 0;
        }
        /* 顶部导航栏 */
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px 15px;
            background: white;
            border-bottom: 1px solid #e0e0e0;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
            overflow: hidden;
            color: #2c3e50;
        }
        
        .header.collapsed {
            height: 0;
            padding: 0 15px;
            border-bottom: none;
            box-shadow: none;
        }
        .header-left {
            display: flex;
            align-items: center;
        }
        .header-left h1 {
            font-size: 1.1rem;
            margin-left: 10px;
            font-weight: 600;
            color: #2c3e50;
        }
        .header-right {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .header-right .icon-btn {
            background: #3498db;
            color: white;
            border: none;
            padding: 8px 12px;
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 0.9rem;
        }
        .header-right .icon-btn:hover {
            background: #2980b9;
            transform: translateY(-1px);
        }
        /* 日期时间显示 */
        .datetime-display {
            display: flex;
            flex-direction: column;
            align-items: center;
            color: #2c3e50;
            font-size: 0.9rem;
            margin-right: 15px;
        }
        .datetime-display .date {
            font-weight: bold;
            margin-bottom: 2px;
        }
        .datetime-display .time {
            font-family: 'Courier New', monospace;
            color: #7f8c8d;
        }
        /* 主容器 */
        .main-container {
            display: flex;
            flex: 1;
            position: relative;
            overflow: hidden;
            height: calc(100vh - 50px);
            min-height: calc(100vh - 50px);
            transition: height 0.3s ease;
        }
        
        .header.collapsed ~ .main-container {
            height: 100vh;
        }
        /* 左侧导航栏 */
        .sidebar {
            width: 160px;
            background-color: #f8f9fa;
            color: #333;
            overflow-y: auto;
            min-width: 140px;
            max-width: 180px;
            transition: transform 0.3s ease;
            z-index: 1000;
            border-right: 1px solid #e9ecef;
        }
        .sidebar.collapsed {
            width: 0;
            min-width: 0;
            max-width: 0;
            overflow: hidden;
            border-right: none;
        }
        /* 侧边栏切换按钮 */
        .sidebar-toggle {
            position: fixed;
            left: 160px;
            top: 50%;
            transform: translateY(-50%);
            background-color: #6c757d;
            color: white;
            border: none;
            border-radius: 0 8px 8px 0;
            padding: 15px 8px;
            cursor: pointer;
            z-index: 1001;
            transition: all 0.3s ease;
            box-shadow: 2px 0 10px rgba(0,0,0,0.2);
        }
        .sidebar-toggle:hover {
            background-color: #5a6268;
        }
        body.sidebar-collapsed .sidebar-toggle {
            left: 0px;
        }
        body:not(.sidebar-collapsed) .sidebar-toggle {
            left: 160px;
        }
        .sidebar-header {
            padding: 15px;
            border-bottom: 1px solid #e9ecef;
            background-color: #ffffff;
        }
        .sidebar-header h2 {
            font-size: 1rem;
            font-weight: 600;
            color: #495057;
        }
        .sidebar-menu {
            padding: 10px 0;
        }
        .menu-item {
            display: flex;
            align-items: center;
            padding: 10px 15px;
            cursor: pointer;
            transition: background-color 0.3s;
            border-left: 3px solid transparent;
            font-size: 0.9rem;
        }
        .menu-item:hover {
            background-color: #e9ecef;
        }
        .menu-item.active {
            background-color: #e3f2fd;
            border-left-color: #2196f3;
        }
        .menu-item i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }
        /* 主内容区域 */
        .content {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
            background-color: #ecf0f1;
            transition: margin-left 0.3s ease;
            margin-left: 0;
            width: calc(100vw - 160px);
            height: calc(100vh - 50px);
            max-height: calc(100vh - 50px);
        }
        body.sidebar-collapsed .content {
            width: 100vw;
            margin-left: 0;
        }
        
        /* 确保配置表单可以完全显示 */
        .config-form {
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            padding: 30px;
            margin-bottom: 30px;
            min-height: fit-content;
        }
        
        /* 页面标题 */
        .page-title {
            font-size: 2rem;
            color: #2c3e50;
            margin-bottom: 10px;
            font-weight: 600;
        }
        
        .page-subtitle {
            color: #666;
            font-size: 1.1rem;
            margin-bottom: 30px;
        }
        
        /* 配置表单 - 已移动到上面 */
        
        .form-section {
            margin-bottom: 30px;
        }
        
        .section-title {
            font-size: 1.3rem;
            color: #2c3e50;
            margin-bottom: 20px;
            font-weight: 600;
            border-bottom: 2px solid #f0f0f0;
            padding-bottom: 10px;
        }
        
        .site-config {
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            background-color: #fafafa;
            min-height: 120px;
            display: flex;
            flex-direction: column;
        }
        
        /* 站点配置容器 */
        #normal-sites-config,
        #summary-sites-config {
            max-height: none;
            overflow: visible;
        }
        
        .site-config-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }
        
        .site-config-title {
            font-size: 1.1rem;
            font-weight: 600;
            color: #2c3e50;
        }
        
        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr 200px;
            gap: 15px;
            align-items: end;
            min-height: 80px;
        }
        
        /* 响应式布局 */
        @media (max-width: 1200px) {
            .form-row {
                grid-template-columns: 1fr;
                gap: 10px;
                align-items: stretch;
            }
            
            .form-group {
                margin-bottom: 10px;
            }
        }
        
        .form-group {
            display: flex;
            flex-direction: column;
        }
        
        .form-group label {
            font-weight: 500;
            margin-bottom: 5px;
            color: #555;
        }
        
        .form-group input {
            padding: 10px 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 0.9rem;
            transition: border-color 0.3s;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: #3498db;
            box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
        }
        
        .checkbox-group {
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .checkbox-group input[type="checkbox"] {
            width: 18px;
            height: 18px;
        }
        
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 0.9rem;
            font-weight: 500;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }
        
        .btn.primary {
            background-color: #3498db;
            color: white;
        }
        
        .btn.primary:hover {
            background-color: #2980b9;
        }
        
        .btn.success {
            background-color: #27ae60;
            color: white;
        }
        
        .btn.success:hover {
            background-color: #229954;
        }
        
        .btn.secondary {
            background-color: #95a5a6;
            color: white;
        }
        
        .btn.secondary:hover {
            background-color: #7f8c8d;
        }
        
        .btn.danger {
            background-color: #e74c3c;
            color: white;
        }
        
        .btn.danger:hover {
            background-color: #c0392b;
        }
        
        .form-actions {
            display: flex;
            gap: 15px;
            justify-content: flex-end;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #e0e0e0;
        }
        
        /* 消息提示 */
        .message {
            padding: 12px 16px;
            border-radius: 6px;
            margin-bottom: 20px;
            display: none;
        }
        
        .message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .test-btn {
            padding: 8px 12px;
            font-size: 0.8rem;
        }
        
        /* 悬浮展开按钮 */
        .floating-expand-btn {
            position: fixed;
            top: 10px;
            right: 20px;
            background: #3498db;
            color: white;
            border: none;
            padding: 8px 12px;
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 0.9rem;
            z-index: 1000;
            display: none;
            box-shadow: 0 2px 8px rgba(0,0,0,0.2);
        }
        
        .floating-expand-btn:hover {
            background: #2980b9;
            transform: translateY(-1px);
        }
        
        .header.collapsed ~ .floating-expand-btn {
            display: block;
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <div class="header">
        <div class="header-left">
            <i class="fas fa-cog"></i>
            <h1>系统配置</h1>
        </div>
        <div class="header-right">
            <div class="datetime-display">
                <div class="date" id="current-date"></div>
                <div class="time" id="current-time"></div>
            </div>
            <button class="icon-btn" onclick="toggleHeader()" title="收起/展开头部">
                <i class="fas fa-chevron-up" id="header-toggle-icon"></i>
            </button>
            <button class="icon-btn" onclick="location.href='/'" title="返回首页">
                <i class="fas fa-home"></i>
            </button>
        </div>
    </div>
    
    <!-- 悬浮的展开按钮 -->
    <button class="floating-expand-btn" onclick="toggleHeader()" title="展开头部">
        <i class="fas fa-chevron-down"></i>
    </button>

    <!-- 主容器 -->
    <div class="main-container">
        <!-- 左侧导航栏 -->
        <div class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <h2>监控系统导航</h2>
            </div>
            <div class="sidebar-menu">
                <div class="menu-item" onclick="location.href='/'">
                    <i class="fas fa-network-wired"></i>
                    <span>监控站点</span>
                </div>
                <div class="menu-item active">
                    <i class="fas fa-cog"></i>
                    <span>系统配置</span>
                </div>
            </div>
        </div>

        <!-- 侧边栏切换按钮 -->
        <button class="sidebar-toggle" id="sidebar-toggle">
            <i class="fas fa-chevron-left"></i>
        </button>

        <!-- 主内容区域 -->
        <div class="content">
            <div class="page-title">系统配置</div>
            <div class="page-subtitle">配置各地监控站点的访问地址和端口</div>
            
            <div id="message" class="message"></div>
            
            <div class="config-form">
                <!-- 普通站点配置 -->
                <div class="form-section">
                    <div class="section-title">站点配置</div>
                    
                    <div id="normal-sites-config">
                        {% set normal_index = 0 %}
                        {% for site in sites %}
                        {% if site.name not in ['汇总数据', '南京汇总数据'] %}
                        <div class="site-config" data-index="normal-{{ normal_index }}">
                            <div class="site-config-header">
                                <div class="site-config-title">{{ site.name }}站点</div>
                            </div>
                            <div class="form-row">
                                <div class="form-group">
                                    <label>主机地址</label>
                                    <input type="text" name="host" value="{{ site.host }}" placeholder="例如: 192.168.1.100">
                                </div>
                                <div class="form-group">
                                    <label>端口号</label>
                                    <input type="text" name="port" value="{{ site.port }}" placeholder="例如: 5000">
                                </div>
                                <div class="form-group">
                                    <div class="checkbox-group">
                                        <input type="checkbox" name="enabled" {% if site.enabled %}checked{% endif %}>
                                        <label>启用</label>
                                    </div>
                                    <button type="button" class="btn secondary test-btn" onclick="testConnection('normal-{{ normal_index }}')">
                                        <i class="fas fa-plug" aria-hidden="true"></i>
                                        测试连接
                                    </button>
                                </div>
                            </div>
                        </div>
                        {% set normal_index = normal_index + 1 %}
                        {% endif %}
                        {% endfor %}
                    </div>
                </div>
                
                <!-- 汇总站点配置 -->
                <div class="form-section">
                    <div class="section-title">汇总站点配置</div>
                    
                    <div id="summary-sites-config">
                        {% set summary_index = 0 %}
                        {% for site in sites %}
                        {% if site.name in ['汇总数据', '南京汇总数据'] %}
                        <div class="site-config" data-index="summary-{{ summary_index }}">
                            <div class="site-config-header">
                                <div class="site-config-title">{{ site.name }}站点</div>
                            </div>
                            <div class="form-row">
                                <div class="form-group">
                                    <label>主机地址</label>
                                    <input type="text" name="host" value="{{ site.host }}" placeholder="例如: 192.168.1.100">
                                </div>
                                <div class="form-group">
                                    <label>端口号</label>
                                    <input type="text" name="port" value="{{ site.port }}" placeholder="例如: 5000">
                                </div>
                                <div class="form-group">
                                    <div class="checkbox-group">
                                        <input type="checkbox" name="enabled" {% if site.enabled %}checked{% endif %}>
                                        <label>启用</label>
                                    </div>
                                    <button type="button" class="btn secondary test-btn" onclick="testConnection('summary-{{ summary_index }}')">
                                        <i class="fas fa-plug" aria-hidden="true"></i>
                                        测试连接
                                    </button>
                                </div>
                            </div>
                        </div>
                        {% set summary_index = summary_index + 1 %}
                        {% endif %}
                        {% endfor %}
                    </div>
                </div>
                
                <div class="form-actions">
                    <button type="button" class="btn secondary" onclick="resetConfig()">
                        <i class="fas fa-undo"></i>
                        重置
                    </button>
                    <button type="button" class="btn success" onclick="saveConfig()">
                        <i class="fas fa-save"></i>
                        保存配置
                    </button>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 更新时间显示
        function updateDateTime() {
            const now = new Date();
            const dateStr = now.toLocaleDateString('zh-CN', {
                year: 'numeric',
                month: '2-digit',
                day: '2-digit'
            });
            const timeStr = now.toLocaleTimeString('zh-CN', {
                hour12: false
            });
            
            document.getElementById('current-date').textContent = dateStr;
            document.getElementById('current-time').textContent = timeStr;
        }
        
        // 侧边栏切换
        document.getElementById('sidebar-toggle').addEventListener('click', function() {
            const sidebar = document.getElementById('sidebar');
            const body = document.body;
            
            sidebar.classList.toggle('collapsed');
            body.classList.toggle('sidebar-collapsed');
            
            const icon = this.querySelector('i');
            if (sidebar.classList.contains('collapsed')) {
                icon.className = 'fas fa-chevron-right';
            } else {
                icon.className = 'fas fa-chevron-left';
            }
        });
        
        // 显示消息
        function showMessage(text, type = 'success') {
            const messageEl = document.getElementById('message');
            messageEl.textContent = text;
            messageEl.className = `message ${type}`;
            messageEl.style.display = 'block';
            
            setTimeout(() => {
                messageEl.style.display = 'none';
            }, 3000);
        }
        
        // 获取配置数据
        function getConfigData() {
            const sites = [];
            // 获取普通站点配置
            const normalSiteConfigs = document.querySelectorAll('#normal-sites-config .site-config');
            // 获取汇总站点配置
            const summarySiteConfigs = document.querySelectorAll('#summary-sites-config .site-config');
            
            // 合并所有站点配置
            const allSiteConfigs = [...normalSiteConfigs, ...summarySiteConfigs];
            
            allSiteConfigs.forEach((config, index) => {
                const host = config.querySelector('input[name="host"]').value;
                const port = config.querySelector('input[name="port"]').value;
                const enabled = config.querySelector('input[name="enabled"]').checked;
                const name = config.querySelector('.site-config-title').textContent.replace('站点', '');
                
                sites.push({
                    name: name,
                    host: host,
                    port: port,
                    enabled: enabled
                });
            });
            
            return { sites: sites };
        }
        
        // 保存配置
        async function saveConfig() {
            try {
                const config = getConfigData();
                
                const response = await fetch('/api/config', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(config)
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showMessage('配置保存成功！', 'success');
                } else {
                    showMessage('配置保存失败：' + result.message, 'error');
                }
            } catch (error) {
                showMessage('配置保存失败：' + error.message, 'error');
            }
        }
        
        // 测试连接
        async function testConnection(index) {
            // 根据索引类型选择查找范围
            let config;
            if (typeof index === 'string' && index.startsWith('summary-')) {
                // 汇总站点配置
                config = document.querySelector(`#summary-sites-config [data-index="${index}"]`);
            } else if (typeof index === 'string' && index.startsWith('normal-')) {
                // 普通站点配置
                config = document.querySelector(`#normal-sites-config [data-index="${index}"]`);
            } else {
                // 兼容旧的数字索引（如果有的话）
                config = document.querySelector(`#normal-sites-config [data-index="${index}"]`) || 
                        document.querySelector(`#summary-sites-config [data-index="${index}"]`);
            }
            
            if (!config) {
                showMessage('无法找到对应的站点配置', 'error');
                return;
            }
            
            const host = config.querySelector('input[name="host"]').value;
            const port = config.querySelector('input[name="port"]').value;
            const siteName = config.querySelector('.site-config-title').textContent.replace('站点', '');
            
            if (!host || !port) {
                showMessage('请先填写主机地址和端口号', 'error');
                return;
            }
            
            try {
                const response = await fetch('/api/test_connection', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ host: host, port: port, name: siteName })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showMessage(result.message, 'success');
                } else {
                    showMessage(result.message, 'error');
                }
            } catch (error) {
                showMessage(`${siteName}(${host}:${port}) 连接测试失败：` + error.message, 'error');
            }
        }
        
        // 重置配置
        function resetConfig() {
            if (confirm('确定要重置配置吗？这将恢复到默认设置。')) {
                location.reload();
            }
        }
        
        // 头部收起/展开功能
        function toggleHeader() {
            const header = document.querySelector('.header');
            const icon = document.getElementById('header-toggle-icon');
            
            header.classList.toggle('collapsed');
            
            if (header.classList.contains('collapsed')) {
                icon.className = 'fas fa-chevron-down';
            } else {
                icon.className = 'fas fa-chevron-up';
            }
        }
        
        // 初始化
        updateDateTime();
        setInterval(updateDateTime, 1000);
    </script>
</body>
</html>